package com.myh.gulimall.product.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.myh.gulimall.product.entity.BrandEntity;
import com.myh.gulimall.product.response.CateBrandRelResp;
import com.myh.gulimall.product.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.myh.gulimall.product.entity.CategoryBrandRelationEntity;
import com.myh.gulimall.product.service.CategoryBrandRelationService;
import com.myh.common.utils.PageUtils;
import com.myh.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author myh
 * @email 2722407149@qq.com
 * @date 2024-01-01 14:39:38
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /*
    * 获取当前品牌关联的所有分类列表
    * */
//    @RequestMapping(value = "/catelog/list", method = RequestMethod.GET)
    @GetMapping("/catelog/list")
    public R catelogList(@RequestParam("brandId") Long brandId){
        List<CategoryBrandRelationEntity> entityList =  categoryBrandRelationService.findCatelogListByBrandId(brandId);
//        List<CateBrandRelResp> resp = entityList.stream().map((entity) -> {
//            CateBrandRelResp relResp = new CateBrandRelResp();
//            relResp.setCatelogId(entity.getCatelogId());
//            relResp.setCatelogName(entity.getCatelogName());
//            return relResp;
//        }).collect(Collectors.toList());
        return R.ok().put("data", entityList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取分类关联的品牌
     * @param catId
     * @return
     */
    @GetMapping("/brands/list")
    public R getBrandsList(@RequestParam("catId") Long catId){
        List<BrandEntity> vos = categoryBrandRelationService.getBrandsByCatId(catId);
        List<BrandVo> collect = vos.stream().map((vo) -> {
            BrandVo brandVo = new BrandVo();
            BeanUtils.copyProperties(vo, brandVo);
            brandVo.setBrandName(vo.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", collect);
    }

}
